#include "UnitTestSwitch.h"
#ifdef UNIT_TEST
#include <limits.h>
#include "gtest/gtest.h"
#include "TiXmlHelper.h"
#include <string>
TEST(TiXmlHelper, CreateConfigXML) {
	TiXmlHelper xmlHelper;
	EXPECT_TRUE(xmlHelper.CreateDoc());
	
	EXPECT_TRUE(xmlHelper.SetChildProperty("Dev", "ID", "00000000000000000001", "Configuration"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Dev", "Protocol", "NVCT", "Configuration"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Dev", "IsGBDevice", "0", "Configuration"));

	EXPECT_TRUE(xmlHelper.SetChildValue("Dev", "name1", "Configuration", 2));
	EXPECT_TRUE(xmlHelper.SetChildValue("Dev", "name2", "Configuration", 3));
	EXPECT_TRUE(xmlHelper.SetChildValue("Dev", "name3", "Configuration", 4));



	EXPECT_TRUE(xmlHelper.SetChildValue("GB28181", "", "Configuration"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("GBDevice", "ID", "34030000001110000003", "GB28181"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("GBDevice", "Pwd", "12345678", "GB28181"));
	
	EXPECT_TRUE(xmlHelper.SetChildProperty("Network", "IP", "192.168.2.25", "GBDevice"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Network", "gateway", "192.168.2.1", "GBDevice"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Network", "Mask", "255.255.255.0", "GBDevice"));

	EXPECT_TRUE(xmlHelper.SetChildProperty("Network", "IP", "192.168.2.25", "GBDevice", 2));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Network", "gateway", "192.168.2.2", "GBDevice", 2));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Network", "Mask", "255.255.255.225", "GBDevice",2));
	xmlHelper.Save("./config.xml");
}

TEST(TiXmlHelper, SetConfigXML) {
	TiXmlHelper xmlHelper;
	EXPECT_TRUE(xmlHelper.Load("./config.xml"));
	EXPECT_TRUE(xmlHelper.SetChildValue("Dev", "NVCT", "Configuration"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Dev", "IP", "192.168.1.1", "Configuration"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("Dev", "ID", "00000000000000000001", "Configuration"));

	EXPECT_TRUE(xmlHelper.SetChildProperty("GBDevice", "ID", "34030000001110000004", "GB28181"));
	EXPECT_TRUE(xmlHelper.SetChildProperty("GBDevice", "Pwd", "12345679", "GB28181"));
	xmlHelper.Save();
}

TEST(TiXmlHelper, ReadConfigXML) {
	TiXmlHelper xmlHelper;
	EXPECT_TRUE(xmlHelper.Load("./config.xml"));

	//Dev Value
	std::string value = xmlHelper.GetChildValue("Dev", "Configuration");
	EXPECT_STRCASEEQ(value.c_str(), "NVCT");

	//Dev ID
	value = xmlHelper.GetChildProperty("Dev", "ID", "Configuration");
	EXPECT_STRCASEEQ(value.c_str(), "00000000000000000001");

	EXPECT_TRUE(xmlHelper.SetChildValue("Dev", "nameName", "Configuration", 2));

	//GBDevice ID
	value = xmlHelper.GetChildProperty("GBDevice", "ID", "GB28181");	
	EXPECT_STRCASEEQ(value.c_str(), "34030000001110000004");

	//GBDevice Pwd
	value = xmlHelper.GetChildProperty("GBDevice", "Pwd", "GB28181");
	EXPECT_STRCASEEQ(value.c_str(), "12345679");

	//Network 1
	value = xmlHelper.GetChildProperty("Network", "IP", "GBDevice");
	EXPECT_STRCASEEQ(value.c_str(), "192.168.2.25");

	value = xmlHelper.GetChildProperty("Network", "gateway", "GBDevice");
	EXPECT_STRCASEEQ(value.c_str(), "192.168.2.1");

	value = xmlHelper.GetChildProperty("Network", "Mask", "GBDevice");
	EXPECT_STRCASEEQ(value.c_str(), "255.255.255.0");


	//Network  2
	value = xmlHelper.GetChildProperty("Network", "IP", "GBDevice",2);
	EXPECT_STRCASEEQ(value.c_str(), "192.168.2.25");

	value = xmlHelper.GetChildProperty("Network", "gateway", "GBDevice",2);
	EXPECT_STRCASEEQ(value.c_str(), "192.168.2.2");

	value = xmlHelper.GetChildProperty("Network", "Mask", "GBDevice", 2);
	EXPECT_STRCASEEQ(value.c_str(), "255.255.255.225");

	xmlHelper.Save();
}
#endif // 
